<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
</head>
      <script type="text/javascript">
        window.addEventListener('message', function(e){
            // console.log('B get message from A ' );
            // console.log(e.data)
            let form = {"serialId": e.data.serialId,"method":e.data.method};
            if(e.data.method == "get"){

                  // console.log("B send message begin");
                  // console.log(e.data);
                  let responseData = {};
                  if(e.data.name){
                    var saveVal = window.localStorage[e.data.name];
                    if(saveVal && undefined != saveVal && "undefined" != saveVal){
                      responseData[e.data.name] = saveVal;
                    }else{
                      responseData[e.data.name] = undefined;
                    }
                  }
                  if(e.data.names){
                    for (var i in e.data.names){
                      let name = e.data.names[i]
                      // console.log(name);
                      var saveVal = window.localStorage[name];
                      // responseData[name] = saveVal;
                      if(saveVal && undefined != saveVal && "undefined" != saveVal){
                        responseData[name] = saveVal;
                      }else{
                        responseData[name] = undefined;
                      }
                    }

                  }

                  Object.assign(form, responseData);
                  e.source.postMessage(form, '*');
            }else if (e.data.method == "save") {
              let obj = e.data.obj;
              Object.keys(obj).forEach(function(key){
                 window.localStorage[key]=obj[key];
            });

                let responseData = obj;
                // console.log("B send message begin");
                Object.assign(form, responseData);
                e.source.postMessage(form, '*');
            }else if (e.data.method == "remove") {
                window.localStorage.removeItem(e.data.name);
                if(e.data.names){
                  for (var i in e.data.names){
                    let name = e.data.names[i]
                    window.localStorage.removeItem(name);
                  }
              }
                let responseData = {};
                // console.log("B send message begin");
                Object.assign(form, responseData);
                e.source.postMessage(form, '*');
            }else{
              // console.log("B send message begin");
              let responseData = {};
              Object.assign(form, responseData);
              e.source.postMessage(form, '*');
            }

        }, false);
      </script>
  </html>

